import { _decorator, Component, Node, tween, Vec3, director } from 'cc';
const { ccclass, property } = _decorator;
import { createMonster } from './createMonster';
import speedUp from './managers/speedData/speedUp';

@ccclass('playerAmmo2')
export class playerAmmo2 extends Component {
    private myTween: any;
    @property(Node)
    private monsters: Node;

    start() {
        globalThis.playerAmmo2 = this;
    }

    yincang() {
        this.node.active = false;
    }

    newNode() {
        this.node.active = true;
    }

    _init() {


        const myNode = this.node;
        const newPosition = new Vec3(0, 0, 0);
        const monsters = this.monsters;
        // console.log(monsters);

        if (monsters.children.length === 0) {


            return;
        }
        // 使用 setPosition 方法设置节点的位置
        myNode.setPosition(newPosition);


        const playTween = () => {

            this.node.active = true;
            var m = 0;
            for (let i = 0; i < monsters.children.length; i++) {
                m = i;

                if (!monsters.children.length) {
                    m != i;
                } else {
                    // const random_number = Math.floor(Math.random() * monsters.children.length) + 1;
                    m = i;
                    var monster = monsters.children[m];
                }
                // console.log(monsters.children.length);

            }
            if (monsters.children.length === 0) {

                let createMonsters = new createMonster();
                globalThis.sceneData.guanQia();
                return;
            }
            var playerx = this.node.parent.position.x;
            var playery = this.node.parent.position.y;
            // console.log(monster);

            var monsterx = monster.position.x;
            var monstery = monster.position.y;
            var playerJulix = monsterx - playerx;
            var playerJuliy = monstery - playery;
            var myNode = this.node;
            var newPosition = new Vec3(0, 0, 0);
            // 使用 setPosition 方法设置节点的位置
            myNode.setPosition(newPosition);

            let speedup = speedUp.getInstance();
            let speed = 2.0
            speed -= speedup.addSpeedFun()*0.01;
            this.myTween = tween(this.node)
                .by(speed, { position: new Vec3(playerJulix, playerJuliy, 0) })
                .by(0.01, { position: new Vec3(-playerJulix, -playerJuliy, 0) })
                .call(() => {
                    console.log(speed);
                    // 继续播放 Tween 动画
                    playTween();
                })
                .start();
        }

        // 开始第一次播放 Tween 动画
        playTween();

    }


}

